<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	public function my_projects(){
		$this->db->select('*');
		$this->db->from('projects');
		$client_id = $this->session->userdata('user_id');
		$this->db->where('client_id = '.$client_id);
		return $this->db->get()->result();	
	}
	
	public function add_client($additional_data){
		$this->db->insert('users',$additional_data);
		return $this->db->insert_id();
	}
	
	public function client_details($insert_details){
		$this->db->insert('client_details',$insert_details);
	}
	
	public function add_client_to_group($client_id){
		$this->db->insert('users_groups',array(
		'user_id' => $client_id,
		'group_id'=> 7,
		));
	}
	
	public function get_clients(){
			$this->db->select('client_details.*,client_details.id as client_id,users.*,users.id as user_id');
			$this->db->from('client_details');
			$this->db->join('users','client_details.client_id = users.id');
			return $this->db->get()->result();
	}
	
	public function get_client_details($id){		
			$this->db->select('client_details.*,users.*');
			$this->db->from('client_details');
			$this->db->where('client_details.id',$id);
			$this->db->join('users','client_details.client_id = users.id');			
			return $this->db->get()->row();
		}
	
	public function update($user_update,$client_update,$client_ID){
		$this->db->update('users',$user_update,array('id'=>$client_ID));
		$this->db->update('client_details',$client_update,array('client_id'=>$client_ID));
	}
	
	public function client_questions($limit=NULL){
		$this->db->select('projects.id as pId,projects.name as pName,tasks.*,task_notes.*');
		$this->db->from('projects');
		$this->db->where('projects.client_id',$this->session->userdata('user_id'));
		if(isset($limit)){
		$this->db->where('task_notes.is_answered',0);
		}
		$this->db->join('tasks','tasks.project_id = projects.id');
		$this->db->join('task_notes','task_notes.task_id = tasks.id AND task_notes.note_type = 2');		
		if(isset($limit)){
		$this->db->limit($limit);	
		}
		$this->db->order_by('task_notes.id','DESC');
		return $this->db->get()->result();		
	}	
	
	public function recent_tasks(){
		//$this->db->group_by('projects.id');
		$this->db->select('projects.id as pId,projects.name as pName,tasks.*');
		$this->db->from('projects');
		$this->db->where('projects.client_id',$this->session->userdata('user_id'));
		$this->db->join('tasks','tasks.project_id = projects.id');			
		$this->db->limit(5);
		$this->db->order_by('tasks.id','DESC');
		return $this->db->get()->result();	
	}
	
	public function recent_projects(){
		$this->db->select('*');
		$this->db->from('projects');
		$client_id = $this->session->userdata('user_id');
		$this->db->where('client_id = '.$client_id);
		$this->db->limit(5);
		return $this->db->get()->result();
	}
}